﻿using System.Data;
using Marbles.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Marbles.CMS.ContentTypes.Article
{
    public class ContentManager : ContentManagerBase
    {
        public Dictionary<string, string> GetPageContent(string PageName)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            Dictionary<string, string> content = new Dictionary<string, string>();
            DataSourceCommand cmd = new DataSourceCommand("SELECT contentid, content FROM PageContent WHERE pageid = @pageid");
            cmd.Parameters.Add("@pageid", PageName);
            DataTable dt = dbc.FetchDataTable(cmd);

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    content.Add(dt.Rows[i]["contentid"].ToString(), dt.Rows[i]["content"].ToString());
                }
            }

            return content;
        }

        public Dictionary<string, string> GetPageContent(Uri uri)
        {
            return GetPageContent(GetPageName(uri));
        }

        public void InsertContent(PageDefinition Definition, Dictionary<string, string> Content)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            InsertDefinition(Definition);

            foreach (string contentid in Content.Keys)
            {
                DataSourceCommand cmd1 = new DataSourceCommand("INSERT INTO PageContent (pageid, contentid, content) VALUES (@pageid, @contentid, @content)");
                cmd1.Parameters.Add("@pageid", Definition.PageName);
                cmd1.Parameters.Add("@contentid", contentid);
                cmd1.Parameters.Add("@content", Content[contentid]);
                dbc.ExecuteCommand(cmd1);
            }
        }

        public void UpdateContent(PageDefinition Definition, Dictionary<string, string> Content)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            UpdateDefinition(Definition);

            DataSourceCommand cmd2 = new DataSourceCommand("DELETE FROM PageContent WHERE pageid = @pageid");
            cmd2.Parameters.Add("@pageid", Definition.PageName);
            dbc.ExecuteCommand(cmd2);


            foreach (string contentid in Content.Keys)
            {
                DataSourceCommand cmd1 = new DataSourceCommand("INSERT INTO PageContent (pageid, contentid, content) VALUES (@pageid, @contentid, @content)");
                cmd1.Parameters.Add("@pageid", Definition.PageName);
                cmd1.Parameters.Add("@contentid", contentid);
                cmd1.Parameters.Add("@content", Content[contentid]);
                dbc.ExecuteCommand(cmd1);
            }
        }

        public void DeleteContent(string PageName)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            DataSourceCommand cmd = new DataSourceCommand("DELETE FROM Page WHERE pageid = @pageid; DELETE FROM PageContent WHERE pageid = @pageid");
            cmd.Parameters.Add("@pageid", PageName);
            dbc.ExecuteCommand(cmd);
        }


    }
}
